package com.hd.controller;

import com.hd.controller.dto.NoticeInfoDto;
import com.hd.pojo.NoticeInfo;
import com.hd.service.NoticeInfoService;
import com.hd.tools.Log;
import com.hd.tools.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * Created by 1 on 2017/12/1.
 */
@Controller
@RequestMapping("/director/noticeInfo")
public class NoticeInfoController {

    private String msg;

    @Resource
    private NoticeInfoService noticeInfoService;

    @ResponseBody
    @RequestMapping("/searchNotice")
    public Pager searchNotice(Integer offset, Integer limit, NoticeInfoDto dto){

        Pager pager = noticeInfoService.searchAllNoticerInfo(offset,limit,dto);
        System.out.println(pager.getRows());
        return pager;
    }

    @ResponseBody
    @PostMapping("/addNoticeInfo")
    public String addNoticeInfo(NoticeInfo noticeInfo){
        noticeInfo.setNoticePubDate(LocalDateTime.now());
        try{
            noticeInfoService.addNoticeInfo(noticeInfo);
            msg = "公告发布成功!";
        }catch (RuntimeException ex){
            msg = "公告发布失败!";
            Log.logger.error(msg,ex);
        }
        return msg;
    }


    @GetMapping("/viewNoticeInfoByNoticeId")
    public String viewNoticeInfoByNoticeId(Integer noticeId, Model model){
        NoticeInfo noticeInfo = noticeInfoService.viewNoticeInfo(noticeId);
        model.addAttribute("noticeInfo",noticeInfo);
        return "director/viewNoticeInfo";
    }



}
